package com.huiyujinrong.qiangdanbao.activity.my;


import com.huiyujinrong.qiangdanbao.BorrowHelpApp;
import com.huiyujinrong.qiangdanbao.DaggerModelComponent;
import com.huiyujinrong.qiangdanbao.model.OrderStatusModel;
import com.huiyujinrong.qiangdanbao.network.Api;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import javax.inject.Inject;

import happyloan.core.RespEvent;

public class OrderStatusPresenter implements OrderStatusContract.Presenter{

    @Inject
    OrderStatusModel model;

    private OrderStatusContract.View view;

    public OrderStatusPresenter(OrderStatusContract.View view){
        this.view=view;
        setDagger2();
    }

    private void setDagger2(){
        DaggerModelComponent.builder().appComponent(BorrowHelpApp.get().getAppComponent()).build().inject(this);
    }

    @Override
    public void start() {
        EventBus.getDefault().register(this);
    }

    @Override
    public void end() {
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void submitOrderStatus(String order_id, int status, String reason) {
        view.disPlay();
        model.submitOrderStatus(order_id,status,reason);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void respEvent(RespEvent<String> event) {
        view.disMiss();
        if (event.getUrl().equals(Api.submitOrderStatus)) {
            switch (event.getCode()) {
                case RespEvent.Code.SUCCEED:
                    //数据返回成功
                    view.showToast("提交成功");
                    view.submitOK();
                    break;
                case RespEvent.Code.FAILURE:
                    //数据返回错误
                    view.showToast(String.valueOf(event.getExtra()));
                    break;
                case RespEvent.Code.ERROR:
                    //数据返回异常
                    view.showToast("因网络原因提交失败，稍后再试！");
                    break;
            }
        }
    }
}
